package yhb

import org.apache.spark.sql.{DataFrame, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}
import yhb.knn.KDTree

import scala.util.Random

/**
  * Created by root on 15-12-18.
  */
object TestKDTree {

  def createDF():DataFrame = {
    val conf = new SparkConf().setAppName("run-algorithm").setMaster("local")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    import sqlContext.implicits._
    val num = 10
    val a1 = new Array[Double](num)
    val a2 = new Array[Double](num)
    val a3 = new Array[Double](num)
    val r = new Random()
    for(i <- a1.indices){
      a1(i) = r.nextDouble()
      a2(i) = r.nextDouble()
      a3(i) = r.nextDouble()
    }

    //sc.parallelize(a1 zip a2).toDF()
    //sc.parallelize(a1 zip a2 zip a3).map(x=>(x._1._1,x._1._2,x._2)).toDF()
    sc.parallelize(Array((0.24326607417926183,0.7082811557357078),
      (0.1806947081906215,0.9587273946211187), (0.9983383119125433,0.49973741144551376),
      (0.9239619185509049,0.6545223738686194), (0.998068755494617,0.7939788859890198),
      (0.09154601595167855,0.6702631783123036), (0.443860992621645,0.6790759136691865),
      (0.8748841495119223,0.7066751816970059), (0.9193108250754026,0.12078437912798068),
      (0.014520267201081793,0.23690272463740591))).toDF()
  }

  def testCreateKDTreeRDD():Unit = {
    val testData = createDF()
    val kdTree = new KDTree(testData,"kd_tree_leaf_node","kd_tree_node")
    val (leafNodeRDD,nodeRDD) = kdTree.createKDTreeRDD()
    println("leafNode : ")
    leafNodeRDD.foreach(println)
    println("---------------------------------------------------------------")
    println("node : ")
    nodeRDD.foreach(println)
  }

  def testSave(): Unit ={
    val testData = createDF()
    val kdTree = new KDTree(testData,"kd_tree_leaf_node","kd_tree_node")
    kdTree.saveKDTree()
  }

  def testSearchPath(point:Array[Double]) ={
    val kdTree = new KDTree("kd_tree_leaf_node","kd_tree_node")
    kdTree.searchPath(point)
  }

  def main(args: Array[String]): Unit = {
    testCreateKDTreeRDD()
//    testSave()
//    println(testSearchPath(Array(0.3808394432707395,0.7213146678359376)))
  }
}
